package com.example.bidc_work_state;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DelayedTaskActivity extends Activity {
	 //URL to get JSON Array
    private String url = "http://113.161.70.47:8013/api/BugnetIssue/GetDelayTasks?projectId=29";
    
    //JSON Node Names 
    private static final String TAG_TITLE = "Title";
    private static final String TAG_STATUS = "status";
    private static final String TAG_DUE_DATE = "DueDate";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delayed_task);		
		/*final TextView delayed_task = (TextView) findViewById(R.id.textview_delayed_task);*/
		ArrayList<HashMap<String, String>> list_task = new ArrayList<HashMap<String,String>>();        
        
        JsonReader json_reader = new JsonReader();
        
/*        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        
        url = url + sdf.format(date);*/
                
        String result = json_reader.readJSONFeed(url);
        
        try {
        	
            JSONArray jsonObject = new JSONArray(result);
            
            for (int i = 0; i < jsonObject.length(); i++)
            {
	            JSONObject temp = jsonObject.getJSONObject(i);
	            
	            String id = temp.getString(TAG_STATUS);
	            String name = temp.getString(TAG_TITLE);
	            String due_date = temp.getString(TAG_DUE_DATE);	            
	            
	            //creating new HashMap
	            HashMap<String, String> map = new HashMap<String, String>();
	            
	            // adding each child node to HashMap key => value
	            map.put(TAG_STATUS, id);
	            map.put(TAG_TITLE, name);
	            map.put(TAG_DUE_DATE, due_date);
	
	            // adding HashList to ArrayList
	            list_task.add(map);
            }
            
        } catch (Exception e) {
            Log.d("ReadWeatherJSONFeedTask", e.getLocalizedMessage());
        } 
        
        
        /**
         * Updating parsed JSON data into ListView
         * */
         try{
        	 
        	 final ListView listview = (ListView) findViewById(R.id.list_view_current_task);    	     	     
        	        
        	 ListAdapter adapter = new SimpleAdapter(this, list_task , R.layout.activity_current_task,
                     new String[] { TAG_STATUS, TAG_TITLE, TAG_DUE_DATE },
                     new int[] { R.id.uid, R.id.name, R.id.due_date });   	 
    				
        	 listview.setAdapter(adapter); 
        	 listview.setOnItemClickListener(new OnItemClickListener() {
          		  @Override
          		  public void onItemClick(AdapterView<?> parent, View view,
          		    int position, long id) {
       /*        		    Toast.makeText(getApplicationContext(),
          		      "Click ListItem Number " + position + id + view, Toast.LENGTH_LONG)
          		      .show();*/
                      // getting values from selected ListItem
                      String single_title = ((TextView) view.findViewById(R.id.name)).getText().toString();
                      String single_duedate = ((TextView) view.findViewById(R.id.due_date)).getText().toString();
                      String single_status = ((TextView) view.findViewById(R.id.uid)).getText().toString();
                      try 
                      {
                      // Starting new intent
                      Intent in = new Intent(getApplicationContext(), SingleItemActivity.class);
                      //Intent in = new Intent().setClass(this, SingleMenuItemActivity.class);
                      in.putExtra(TAG_TITLE, single_title);
                      in.putExtra(TAG_DUE_DATE, single_duedate);
                      in.putExtra(TAG_STATUS, single_status);
                      /*in.putExtra(TAG_ID, id);*/
                      startActivity(in);
                      }
                      catch (Exception e)
                      {
                      	Log.d("error", e.getLocalizedMessage());
                      }
          		  }
          		}); 
         } catch (Exception e)
         {
        	 e.printStackTrace();
         }    	 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.delayed_task, menu);
		return true;
	}

}
